package com.marvels.financial.consum.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.dto.MclaRelationInfoDTO;
import com.marvels.financial.consum.FinancialConsumerLoanApplyApi;
import com.marvels.financial.loan.dto.CheckBillDTO;
import com.marvels.financial.loan.dto.LaAttachmentDto;
import com.marvels.financial.loan.dto.MclaLoanApplyLinkmanDTO;
import com.marvels.financial.loan.dto.MclaUserBankDTO;
import com.marvels.loanapply.api.LoanapplyAttachAchieveApi;
import com.marvels.loanapply.api.LoanapplyCheckBillApi;
import com.marvels.loanapply.api.MclaLinkmanMobileApi;
import com.marvels.loanapply.api.MclaUserBankApi;
import com.marvels.loanapply.api.RelationInfoApi;

import cn.hutool.core.bean.BeanUtil;


@Component
public class FinancialConsumerLoanApplyImpl implements FinancialConsumerLoanApplyApi{

	@Autowired
	private LoanapplyAttachAchieveApi attachAchieveApi;
	
	@Autowired
	private MclaLinkmanMobileApi linkManMobile;
	
	@Autowired
	private MclaUserBankApi bankApi;
	
	@Autowired
	private RelationInfoApi relationApi;
	
	@Autowired
	private LoanapplyCheckBillApi checkBillApi;
	
	@Override
	public List<LaAttachmentDto> achieveAttachInfo( LaAttachmentDto laAttachmentDto) {
		try {
			List<com.marvels.loanapply.dto.LaAttachmentDto> apiResult = attachAchieveApi.achieveAttachInfo(laAttachmentDto.getAtmtTypes(), laAttachmentDto.getLoanIds(),laAttachmentDto.getFileIds());
			List<LaAttachmentDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), LaAttachmentDto.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.achieveAttachInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.achieveAttachInfo error", e);
			return null;
		}
	}

	@Override
	public List<LaAttachmentDto> compactAttach( LaAttachmentDto laAttachmentDto) {
		try {
			List<com.marvels.loanapply.dto.LaAttachmentDto> apiResult = attachAchieveApi.compactAttach(laAttachmentDto.getFileIds(), laAttachmentDto.getLoanIds());
			List<LaAttachmentDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), LaAttachmentDto.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.compactAttach direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.compactAttach error", e);
			return null;
		}
	}

	@Override
	public List<MclaLoanApplyLinkmanDTO> selectByLoanId(MclaLoanApplyLinkmanDTO dto) {
		try {
			com.marvels.loanapply.dto.MclaLoanApplyLinkmanDTO param = new com.marvels.loanapply.dto.MclaLoanApplyLinkmanDTO();
			BeanUtil.copyProperties(dto, param);
			List<com.marvels.loanapply.dto.MclaLoanApplyLinkmanDTO> apiResult = linkManMobile.selectByLoanId(param.getLoanId());
			List<MclaLoanApplyLinkmanDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), MclaLoanApplyLinkmanDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.selectByLoanId direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.selectByLoanId error", e);
			return null;
		}
	}

	@Override
	public MclaUserBankDTO selectCardType(MclaUserBankDTO dto) {
		try {
			com.marvels.loanapply.dto.MclaUserBankDTO param = new com.marvels.loanapply.dto.MclaUserBankDTO();
			BeanUtil.copyProperties(dto, param);
			com.marvels.loanapply.dto.MclaUserBankDTO apiResult = bankApi.selectCardType(param);
			MclaUserBankDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), MclaUserBankDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.selectCardType direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.selectCardType error", e);
			return null;
		}
	}

	@Override
	public List<MclaRelationInfoDTO> shipList(MclaRelationInfoDTO dto) {
		try {
			com.marvels.loanapply.dto.MclaRelationInfoDTO param = new com.marvels.loanapply.dto.MclaRelationInfoDTO();
			BeanUtil.copyProperties(dto, param);
			List<com.marvels.loanapply.dto.MclaRelationInfoDTO> apiResult=relationApi.relationList(param);
			List<MclaRelationInfoDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), MclaRelationInfoDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.shipList direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.shipList error", e);
			return null;
		}
	}
	
	/**
	 * 获取该时间段内指定续贷用户
	 * @param param
	 * @return
	 */
	@Override
	public String  selectContinueLoanUser(CheckBillDTO dto) {
		try {
			String startDate = dto.getStartDate();
			String endDate = dto.getEndDate();
			String result = checkBillApi.selectContinueLoanUser(startDate,endDate);		
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.selectContinueLoanUser direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.selectContinueLoanUser error", e);
			return null;
		}
	}
	@Override
	public String synchroMoneyHandOutTime(String loanId){
		try {
			String reloanFlag =  checkBillApi.synchroMoneyHandOutTime(loanId);		
			MarvelsLogUtil.getInstance().info("FinancialConsumerLoanApplyApi.synchroMoneyHandOutTime direct call success!");
			return reloanFlag;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerLoanApplyApi.synchroMoneyHandOutTime error", e);
			return null;
		}
	}
}
